//
//  UserViewController.m
//  Cooking
//
//  Created by dllo on 15/8/22.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "UserViewController.h"
#import "DCPathButton.h"
#import "AppMacro.h"
#import <StoreKit/StoreKit.h>
#import "AboutUsViewController.h"
#import "CollectViewController.h"
#import "SaveTools.h"
#import "AppDelegate.h"
#import "UIImageView+WebCache.h"

@interface UserViewController ()<DCPathButtonDelegate, SKStoreProductViewControllerDelegate, UIAlertViewDelegate>

@property (retain, nonatomic) DCPathButton *dcPathButton;
@property (retain, nonatomic) UIButton     *collectButton;
@property (retain, nonatomic) UIButton     *cacheButton;
@property (retain, nonatomic) UIButton     *settingButton;
@property (retain, nonatomic) UIButton     *aboutButton;
@property (retain, nonatomic) UIButton     *gradeButton;
@property (assign, nonatomic) BOOL         judge;
@property (retain, nonatomic) UIView       *nightView;
@end

@implementation UserViewController

- (void)dealloc {
    [_dcPathButton release];
    [_collectButton release];
    [_cacheButton release];
    [_settingButton release];
    [_aboutButton release];
    [_gradeButton release];
    [_nightView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.judge = YES;
    }
    return self;
}

//- (void)viewWillAppear:(BOOL)animated {
//    self.navigationController.navigationBar.hidden = YES;
//
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    self.navigationController.navigationBar.hidden = NO;
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:255 / 255.0 green:125 / 255.0 blue:67 / 255.0 alpha:1];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationItem.title = @"我的";
    
    UIImage *image = [UIImage imageNamed:@"backGround.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.alpha = 0.4;
    imageView.frame = CGRectMake(0, 0, WIDTH ,HEIGHT);
    [self.view addSubview:imageView];
    [imageView release];

    [self createButton];
    [self createView];

}
#pragma mark - 
#pragma mark 夜间
- (void)createView {
    self.nightView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.nightView.backgroundColor = [UIColor blackColor];
    self.nightView.alpha = 0.3;
    self.nightView.userInteractionEnabled = NO;
}


#pragma mark -
#pragma mark appStore评论
- (void)evaluate{
    
    //初始化控制器
    SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init];
    //设置代理请求为当前控制器本身
    storeProductViewContorller.delegate = self;
    //加载一个新的视图展示
    [storeProductViewContorller loadProductWithParameters:
     //appId唯一的
     @{SKStoreProductParameterITunesItemIdentifier : @"587767923"} completionBlock:^(BOOL result, NSError *error) {
         //block回调
         if (error) {
             NSLog(@"error %@ with userInfo %@",error,[error userInfo]);
         } else {
             //模态弹出appstore
             [self presentViewController:storeProductViewContorller animated:YES completion:^{
                 
             }
           ];
         }
     }];
}

//取消按钮监听
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//旋转按钮
- (void)createButton {

    self.view.backgroundColor = [UIColor whiteColor];
    self.dcPathButton = [[DCPathButton alloc]
                         initDCPathButtonWithSubButtons:5
                         totalRadius:130
                         centerRadius:65
                         subRadius:45                                  centerImage:@"iconfont-yingyong_1"
                         centerBackground:nil
                         subImages:^(DCPathButton *dc){
                             [dc subButtonImage:@"iconfont-shoucang" withTag:0];
                             [dc subButtonImage:@"iconfont-qinglihuancun" withTag:1];
                             [dc subButtonImage:@"iconfont-yejian" withTag:2];
                             [dc subButtonImage:@"iconfont-guanyuwomen" withTag:3];
                             [dc subButtonImage:@"iconfont-yijian" withTag:4];
                         }
                         subImageBackground:nil
                         inLocationX:0 locationY:(HEIGHT - 64 - 49) / 2 toParentView:self.view];
    self.dcPathButton.delegate = self;
}

- (void)button_0_action {
    NSLog(@"我的收藏");
    CollectViewController *collectVC = [[CollectViewController alloc] init];
    [self.navigationController pushViewController:collectVC animated:YES];
}
- (void)button_1_action {
    NSLog(@"清理缓存");
    
    UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"清空缓存" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSFileManager *manger = [NSFileManager defaultManager];
        NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
        NSString *path = [directoryPath stringByAppendingPathComponent:@"myCaches"];
        NSLog(@"%@", path);
        
        //计算SDWebImage缓存大小,正常得到的是比特单位,要换算成MB
        CGFloat sdWebImageSize = [[SDImageCache sharedImageCache] getSize] / 1024.0 / 1024.0;
        
        //判断本身的缓存大小,再加上SDWebImage的缓存,一起清除
        if ([manger fileExistsAtPath:path]) {
            CGFloat size = [manger attributesOfItemAtPath:path error:nil].fileSize;
            CGFloat newStr = (size / 1024) + sdWebImageSize;
            
            NSString *str = [NSString stringWithFormat:@"清空缓存%.2f M", newStr];
//            NSString *str = [NSString stringWithFormat:@"清理缓存(%.2fK)",newStr * 1024];
            //之前的写法,后来修改成上面CGfolat类型,为了实现显示M的状态.
            //            long size=[manger attributesOfItemAtPath:path error:nil].fileSize;
            //            NSString *str = [NSString stringWithFormat:@"清空缓存%ld k", size];
            NSLog(@"%@", str);
            [manger removeItemAtPath:path error:nil];
            //清理SDWebImage缓存
            [[SDImageCache sharedImageCache] clearDisk];
            //可有可无
            [[SDImageCache sharedImageCache] clearMemory];
            
            UIAlertController *clearAlter = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *clear = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            }];
            [clearAlter addAction:clear];
            [self presentViewController:clearAlter animated:YES completion:^{
                
            }];
        } else {
            NSString *str = [NSString stringWithFormat:@"清空缓存0 M"];
            NSLog(@"%@", str);
            [manger removeItemAtPath:path error:nil];
            
            UIAlertController *clearAlter = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *clear = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
            }];
            [clearAlter addAction:clear];
            [self presentViewController:clearAlter animated:YES completion:^{
                
            }];
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        
    }];
    [alter addAction:okAction];
    [alter addAction:cancelAction];
    [self  presentViewController:alter animated:YES completion:^{
        
    }];
    
}
- (void)button_2_action {
    NSLog(@"我的设置");
    if (self.judge) {
        AppDelegate *app = [[UIApplication sharedApplication] delegate];
        [app.window addSubview:self.nightView];
        self.judge = NO;
    } else {
        [self.nightView removeFromSuperview];
        self.judge = YES;
    }
}
- (void)button_3_action {
    NSLog(@"关于我们");
    AboutUsViewController *aboutVC = [[AboutUsViewController alloc] init];
    [self.navigationController pushViewController:aboutVC animated:YES];
    [aboutVC release];
}
- (void)button_4_action {
    NSLog(@"给我打分");

#warning 版本迭代
    //版本迭代时候才能进行AppStore评论,功能已经实现,等到上线后获取应用ID,就可以评论了.
//        [self evaluate];
    //做个电话技术支持上线用
    
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您将要拨通建议专线" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alterView.delegate = self;
    [alterView show];
    [alterView release];
  
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    //判断确定,拨通电话
    if (buttonIndex == 0) {
        NSLog(@"取消");
    } else {
        NSString *allString = @"tel://15566850795";
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:allString]];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
